import jdk.nashorn.internal.codegen.CompilerConstants;
import net.sf.cglib.proxy.*;

import java.lang.reflect.Method;

class Target {
  private String result1 = "haha";
  private String result2 = "xixi";
  public void setResult1(String result1) {
    this.result1 = result1;
  }
  public void setResult2(String result2) {
    this.result2 = result2;
  }
  public String method1() {
    System.out.println("method1");
    return result1;
  }
  public String method2() {
    System.out.println("method2");
    return result2;
  }
}

class TargetProxy implements MethodInterceptor{
  @Override
  public Object intercept(Object object, Method method, Object[] params,
                   MethodProxy proxy) throws Throwable{
    System.out.println("start");
    Object result = proxy.invokeSuper(object, params);
    System.out.println("end");
    return result;
  }
}
class TargetMethodFilter implements CallbackFilter {
  @Override
  public int accept(Method method) {
    if(method.getName().equals("method1")) {
      return 0;
    }
    return 1;
  }
}
public class Cglib {
  public static void main(String[] args){
    Enhancer enhancer = new Enhancer();
    enhancer.setSuperclass(Target.class);
    CallbackFilter callbackFilter = new TargetMethodFilter();
    Callback[] callbacks = new Callback[]{new TargetProxy(), NoOp.INSTANCE};
    enhancer.setCallbacks(callbacks);
    enhancer.setCallbackFilter(callbackFilter);
    Target target = (Target)enhancer.create();
    Target target1 = (Target) enhancer.create();
    System.out.println(target.method1());
    System.out.println(target1.method1());
    target.setResult1("new result1");
    System.out.println(target.method1());
    System.out.println(target1.method1());
  }
}
